using NLog;
using NLog.Targets;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace DataService.Helper
{
    public class LogHelper
    {
        /// 
        /// 实例化nLog，即为获取配置文件相关信息
        /// 
        private NLog.Logger _logger;
        public LogHelper()
        {
            _logger = LogManager.GetCurrentClassLogger();
        }
        public LogHelper(string name)
        {
            _logger = LogManager.GetLogger(name);
        }

        private void InitLogConfig(string name)
        {
            try
            {
                foreach (var item in _logger.Factory.Configuration.AllTargets)
                {
                    if (item.GetType().Name == "FileTarget")
                    {
                        FileTarget target = (FileTarget)item;
                        target.FileName = target.FileName.ToString().Replace("logs/$", $"logs/{name}/$");
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex.Message);
            }
        }

        private static LogHelper _obj;

        public static LogHelper _
        {
            get => _obj ?? (new LogHelper());
            set => _obj = value;
        }

        #region Debug，调试
        public void Debug(string msg)
        {
            _logger.Debug(msg);
        }

        public void Debug(string msg, Exception err)
        {
            _logger.Debug(err, msg);
        }
        #endregion

        #region Info，信息
        public void Info(string msg)
        {
            _logger.Info(msg);
        }

        public void Info(string msg, Exception err)
        {
            _logger.Info(err, msg);
        }
        #endregion

        #region Warn，警告
        public void Warn(string msg)
        {
            _logger.Warn(msg);
        }

        public void Warn(string msg, Exception err)
        {
            _logger.Warn(err, msg);
        }
        #endregion

        #region Trace，追踪
        public void Trace(string msg)
        {
            _logger.Trace(msg);
        }

        public void Trace(string msg, Exception err)
        {
            _logger.Trace(err, msg);
        }
        #endregion

        #region Error，错误
        public void Error(string msg)
        {
            _logger.Error(msg);
        }

        public void Error(string msg, Exception err)
        {
            _logger.Error(err, msg);
        }
        #endregion

        #region Fatal,致命错误
        public void Fatal(string msg)
        {
            _logger.Fatal(msg);
        }

        public void Fatal(string msg, Exception err)
        {
            _logger.Fatal(err, msg);
        }
        #endregion
    }
}
